package edu.fiu.cis.personalize.server.berkDB.serializers;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.sleepycat.bind.tuple.TupleBinding;
import com.sleepycat.bind.tuple.TupleInput;
import com.sleepycat.bind.tuple.TupleOutput;

import edu.fiu.cis.personalize.server.graph.Node;
import edu.fiu.cis.personalize.server.graph.store.Edges;

public class EdgesSerializer extends TupleBinding<Edges<Node>>{

	private MapSerializer<Integer, Double> hmSerializer = new MapSerializer<Integer, Double>(){

		@Override
		protected Map<Integer, Double> createInstance() {
			return new ConcurrentHashMap<Integer, Double>(); 
		}

		@Override
		protected Integer readKey(TupleInput ti) {
			return ti.readInt();
		}

		@Override
		protected Double readValue(TupleInput ti) {
			return ti.readDouble();
		}

		@Override
		protected void writeKey(TupleOutput to, Integer key) {
			to.writeInt(key);
		}

		@Override
		protected void writeValue(TupleOutput to, Double value) {
			to.writeDouble(value);
		}};
	
	@Override
	public Edges<Node> entryToObject(TupleInput ti) {
		Map<Integer, Double> inLinks = hmSerializer.entryToObject(ti);
		Map<Integer, Double> outLinks = hmSerializer.entryToObject(ti);
		return Edges.load(inLinks,outLinks);
	}

	@Override
	public void objectToEntry(Edges<Node> es, TupleOutput to) {
		hmSerializer.objectToEntry(es.getInLinksMap(), to);
		hmSerializer.objectToEntry(es.getOutLinksMap(), to);
		
	}

}
